<?php
/**
 * [模型]会员
 * 
 * @version $Id: User.php 289 2012-05-02 08:40:09Z linsir123 $
 */
class modelUser 
{
	/**
	 * @var 数据模型
	 */
	public $dbUser;
	
	/**
	 * @var 数据模型
	 */
	public $dbProfile;
	
	/**
	 * @var 数据模型
	 */
	public $dbAccount;
	
	/**
	 * @var 用户信息
	 */
	public $data = array();
	
	/**
	 * @var 用户ID
	 */
	public $uId = 0;
	
	/**
	 * @var 用户类型
	 */
	public $type = null;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		@session_start();
		
		///
		$this->_init();
		
		///
		$this->dbUser = D::user();
		$this->dbAccount = D::account();
		$this->dbProfile = D::profile();
	}
	
	/**
	 * 使用外部用户进行登录
	 */
	public function loginOauth(& $model)
	{
		if ($this->isLogin()) 
			return ;
			
		///
		$key = $model->getKey();
		$type = $model->getType();
		$row = $this->dbUser->getByOauth($type, $key);
		if ( ! empty($row)) {
			$row['username'] = $this->dbProfile->getBy('uid', $row['id'], 'nick');
			
			///
			$this->_login($row);
		} else 
			$this->_register(array(
				'type'=>$type, 
				'oauth_key'=>$key, 
				'nick'=>$model->getNick()
			));
			
		echo '<script language="javascript">window.opener.location.reload();window.close();</script>';
	}
	
	/**
	 * 登录
	 */
	public function login()
	{
		if ($this->isLogin()) 
			return ;
			
		///
		$userName = trim($_POST['username']);
		$password = trim($_POST['password']);
		$rememberMe = intval($_POST['rememberMe']);
		if ( ! empty($userName) && ! empty($password)) {
			$row = $this->dbUser->getBy('username', $userName);
			if ( ! empty($row)) {
				if ($row['password'] == md5($password)) {
					$this->_login($row);
					
					///
					if ($rememberMe) 
						setcookie ('rememberMe', $userName, time() + 86400 * 7, '/');
					else 
						setcookie ('rememberMe', '', time(), '/');
						
					return '10000|'.$this->uId;
				}
			}
			return '10001|用户名或密码错误';
		}
		return '10001|请输入用户名和密码';
	}
	
	/**
	 * 注册
	 */
	public function register()
	{
		if ($this->isLogin()) 
			return ;
			
		///
		$userName = trim($_POST['username']);
		$password = trim($_POST['password']);
		$password2 = trim($_POST['password2']);
		if ($userName && $password && strcmp($password, $password2) == 0) {
			$row = $this->dbUser->getBy('username', $userName);
			if (empty($row)) {
				$this->_register(array(
					'username'=>$userName, 
					'password'=>md5($password)
				));
				
				return '10000|'.$this->uId;
			}
			return '10001|该用户名已被注册';
		}
		return '10001|数据有误';
	}
	
	/**
	 * 修改密码
	 */
	public function modifyPassword()
	{
		$password = trim($_POST['password']);
		$newPass = trim($_POST['newpassword']);
		$newPass2 = trim($_POST['newpassword2']);
		if ($password && $newPass && strcmp($newPass, $newPass2) == 0) {
			$row = $this->dbUser->getById($this->uId);
			if ($row['password'] == md5($password)) {
				$this->dbUser->updateById($this->uId, array('password'=>md5($newPass)));
				
				return '修改成功';
			}
			return '旧密码有误';
		}
		return '数据有误';
	}
	
	/**
	 * 登出
	 */
	public function logout()
	{
		@session_destroy();
		return true;
	}
	
	/**
	 * 是否登录
	 */
	public function isLogin()
	{
		return ! empty($this->data);
	}
	
	/**
	 * 设置登录信息
	 */
	public function setSession($params = array())
	{
		@session_start();
		$default = array('id'=>0, 'username'=>'', 'type'=>'', 'login_time'=>0, 'login_times'=>0);
		foreach ($default as $k => $v) {
			if (isset($params[$k]) && ! empty($params[$k])) 
				$default[$k] = $params[$k];
		}
		$default['login_time'] = empty($default['login_time']) ? '-' : 
			date('Y-m-d H:i:s', $default['login_time']);
			
		///
		$_SESSION['user'] = $default;
		$this->_init();
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 登录
	 */
	private function _login($row)
	{
		$row['login_time'] = time();
		$row['login_times']++;
			
		///
		$this->dbUser->updateById($row['id'], $row, array('login_time', 'login_times'));
		$this->setSession($row);
	}
	
	/**
	 * 注册
	 */
	private function _register($data)
	{
		$data['create_time'] = time();
		$fields = array('username','password','type','oauth_key','create_time');
		$uId = $this->dbUser->insert($data, $fields);
		
		///
		if ( ! empty($uId)) {
			$this->setSession(array(
				'id'=>$uId, 
				'username'=>isset($data['nick'])?$data['nick']:$data['username']
			));
				
			///
			$data['uid'] = $uId;
			$this->dbProfile->insert($data, array('uid','nick'));
			$this->dbAccount->insert($data, array('uid'));
		}
	}
	
	/**
	 * 初始化用户信息
	 */
	private function _init()
	{
		if (isset($_SESSION['user'])) {
			$this->data = $_SESSION['user'];
			$this->uId = intval($this->data['id']);
			$this->type = trim($this->data['type']);
		}
	}
}
